var t = getApp();

Page({
  data: {
    onPatientFlag: !1,

    patientList: {

    },
    searchPersonList: [],
    arr:[]
  },
  onLoad: function (options) {
    var that = this
    var zid = options.zid
    var sourceType = options.sourceType
    t.util.request({
      'url': 'entry/wxapp/Yishuo.listliebiao',
      data: {
        zid: zid,
      },
      success: function (res) {
        console.log(res)
        let list = res.data;
        list.data.map(function (i) {
          i['groupCheck'] = !0;
          i.user.map(function (j) {
            j['patientCheck'] = !0;
          })
        })
        let weifenzu = res.data.weifenzu;
        weifenzu.map(function (i) {
          i['patientCheck'] = !0;
        })
         console.log(list)
         console.log(weifenzu)
        that.setData({
          patientList: list.data,
          weifenzu: weifenzu
        })
      }
    })
    that.setData({
      zid: zid,
      sourceType: sourceType
    })
  },
  onReady: function () {},
  onShow: function () {
    wx.getStorageSync("patientList") && this.setData({
      patientList: wx.getStorageSync("patientList")
    });
  },
  onHide: function () {},
  onUnload: function () {
    1 != this.data.sourceType && wx.removeStorageSync("patientList");
  },
  groupUnfold: function (t) {
    console.log(t);
    var a = parseInt(t.currentTarget.dataset.index);
    this.data.patientList.groups[a].groupUnfoldFlag ? (this.data.patientList.groups[a].groupUnfoldFlag = !1,
      this.setData({
        patientList: this.data.patientList
      })) : (this.data.patientList.groups[a].groupUnfoldFlag = !0, this.setData({
      patientList: this.data.patientList
    }));
  },
  // 全选
  All:function(e){
    var index =e.currentTarget.dataset.index
    var patientList = this.data.patientList;
    patientList[index].groupCheck = !patientList[index].groupCheck
    if(patientList[index].groupCheck==true){
      patientList[index].user.map((v,k)=>{
        patientList[index].user[k].patientCheck = true
      })
    }else{
      patientList[index].user.map((v,k)=>{
        patientList[index].user[k].patientCheck = false
      })
    }
    console.log(patientList)
    this.setData({
      patientList:patientList
    })
  },
  //已分组单选
  checkPatient: function (e) {
    console.log(e)
    let that = this;
    let patientList = that.data.patientList;
    console.log(patientList)
    let index = e.currentTarget.dataset.index
    let groupindex = e.currentTarget.dataset.groupindex
    let targettype = e.currentTarget.dataset.targettype
    var itemIndex = e.currentTarget.dataset.itemindex
    var arr =[]
    if(targettype == 2){
      patientList[index].user[itemIndex].patientCheck = ! patientList[index].user[itemIndex].patientCheck
      patientList[index].user.map((v,k)=>{
        if(patientList[index].user[k].patientCheck == false){
          patientList[index].groupCheck = false
        }else{
          patientList[index].groupCheck = true

        }
      })
    }
    //选择未分组成员
    let weifenzu = that.data.weifenzu;
    if (targettype == 3) {
      // console.log(weifenzu)
      weifenzu[index].patientCheck = !weifenzu[index].patientCheck;
    }
    console.log(that.data.arr)
    that.setData({
      patientList: patientList,
      weifenzu: weifenzu
    });
  },
  nextBtn: function () {
    let that = this;
    console.log(that.data.zid)
    let patientList = that.data.patientList;
    let groups=[]
    for(var i=0;i<patientList.length;i++){
      groups = patientList[i].user
    }
    let weifenzu = that.data.weifenzu;
    let weiArr = [];
    let userArr = [];
    groups.map((v)=>{
      if(v.patientCheck == true){
        userArr.push(v.u_id)
      }
    })
    console.log(weifenzu)
    weifenzu.map(function (i) {
      if(i.patientCheck == true){
        weiArr.push(i.u_id)
      }
    })
    console.log('已分组成员ID：' + userArr)
    console.log('未分组成员ID：' + weiArr)
    let arr = userArr.concat(weiArr);
    console.log(arr)
    wx.setStorageSync("patientList", this.data.arr), wx.navigateTo({
      url: "/hyb_yl/backstageLife/pages/publishSaid/publishSaid?arr=" + arr + '&zid=' + that.data.zid
    });
  },
  saveBtn: function () {
    wx.setStorageSync("patientList", this.data.patientList), wx.navigateBack({
      delta: 1
    });
  },
  search: function (a) {
    var e = this,
      i = a.detail.value;
    this.setData({
      searchFlag: !0,
      searchOnPatent: !1
    }), 0 != i.replace(/(^\s*)|(\s*$)/g, "").length ? (1 == this.data.patientType && (this.data.searchUrl = "doctor/manage/searchMyPatient",
      this.data.searchData = {
        token: this.data.token,
        keyWord: i
      }), 2 == this.data.patientType && (this.data.searchUrl = "doctor/team/manage/searchMyPatient",
      this.data.searchData = {
        token: this.data.token,
        teamId: this.data.teamId,
        keyWord: i
      }), wx.request({
      url: t.globalData.dic + e.data.searchUrl,
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: e.data.searchData,
      success: function (t) {
        console.log(t), 200 == t.data.code && (t.data.data.length > 0 && null != t.data.data ? (t.data.data.forEach(function (t) {
          if (null != e.data.patientList.groups && e.data.patientList.groups.length > 0)
            for (i = 0; i < e.data.patientList.groups.length; i++)
              if (null != e.data.patientList.groups[i].patients && e.data.patientList.groups[i].patients.length > 0)
                for (var a = 0; a < e.data.patientList.groups[i].patients.length; a++)
                  if (e.data.patientList.groups[i].patients[a].userId == t.userId) {
                    t.patientCheck = e.data.patientList.groups[i].patients[a].patientCheck;
                    break;
                  }
          if (null != e.data.patientList.unGroupPatientList && e.data.patientList.unGroupPatientList.length > 0)
            for (var i = 0; i < e.data.patientList.unGroupPatientList.length; i++)
              if (e.data.patientList.unGroupPatientList[i].userId == t.userId) {
                t.patientCheck = e.data.patientList.unGroupPatientList[i].patientCheck;
                break;
              }
        }), e.setData({
          searchPersonList: t.data.data,
          searchOnPatent: !1
        })) : e.setData({
          searchOnPatent: !0,
          searchPersonList: t.data.data
        }));
      }
    })) : this.setData({
      searchFlag: !1,
      searchOnPatent: !1
    });
  }
});